<?php
namespace common\components\services\message;

use common\models\datas\MessageConfigItem;
use common\models\datas\MessageLog;
use yii\base\Component;
use yii\base\Object;
use yii\db\Query;
use common\components\helpers\Fun;
use yii\web\NotFoundHttpException;

class MessageManage extends Component
{
    public function send($id, $mode, $level)
    {
        $this->getType($id, $mode, $level);
    }

    public function getType($id, $mode, $level)
    {
        $item = MessageLog::findOne([$id]);
        if ($item == null) {
            throw new NotFoundHttpException(\Yii::t('common', '信息不存在'));
        }
        $send_way = unserialize($item->send_way);
        if (in_array('sys', $send_way)) {
            $this->sendSys($id, $mode, $level);
        }

        if (in_array('mail', $send_way)) {
            $this->sendEmail($id, $mode, $level);
        }

        if (in_array('wechat', $send_way)) {
            $this->sendWechat($id, $mode, $level);
        }

        if (in_array('sms', $send_way)) {
            $this->sendSms($id, $mode, $level);
        }
        if (in_array('jpush', $send_way)) {
            $this->sendJpush($id, $mode, $level);
        }
    }

    public function sendSys($id, $mode = false, $level = 0)
    {
//        if ($mode == false) {
            $mailObject = \Yii::createObject(['class' => SysMessage::className()]);
            $mailObject->send($id,Fun::getSiteId());
//        } else {
//            $data['type'] = 'message';
//            $data['level'] = $level;
//            $data['data'] = [
//                'messagelog_id' => $id,
//                'message_type' => 'sys'
//            ];
//            \Yii::$app->swooleClient->send($data);
//        }
    }

    public function sendJpush($id, $mode = true, $level = 0)
    {
        if ($mode == false) {
            $jpushObject = \Yii::createObject(['class' => JpushMessage::className()]);
            $jpushObject->send($id,Fun::getSiteId());
        } else {
            $data['type'] = 'message';
            $data['level'] = $level;
            $data['site_id'] = Fun::getSiteId();
            $data['data'] = [
                'messagelog_id' => $id,
                'message_type' => 'email'
            ];
            \Yii::$app->swooleClient->send($data);
        }
    }

    public function sendEmail($id, $mode = true, $level = 0)
    {
        if ($mode == false) {
            $mailObject = \Yii::createObject(['class' => MailMessage::className()]);
            $mailObject->send($id,Fun::getSiteId());
        } else {
            $data['type'] = 'message';
            $data['level'] = $level;
            $data['site_id'] = Fun::getSiteId();
            $data['data'] = [
                'messagelog_id' => $id,
                'message_type' => 'email'
            ];
            \Yii::$app->swooleClient->send($data);
        }
    }


    public function sendWechat($id, $mode = true, $level = 0)
    {
        if ($mode == false) {
            $wechatObject = \Yii::createObject(['class' => WechatMessage::className()]);
            $wechatObject->send($id,Fun::getSiteId());
        } else {
            $data['type'] = 'message';
            $data['level'] = $level;
            $data['site_id'] = Fun::getSiteId();
            $data['data'] = [
                'messagelog_id' => $id,
                'message_type' => 'wechat'
            ];
            \Yii::$app->swooleClient->send($data);
        }
    }

    public function sendSms($id, $mode = true, $level = 0)
    {
        if ($mode == false) {
            $smsObject = \Yii::createObject(['class' => SmsMessage::className()]);
            $smsObject->send($id,Fun::getSiteId());
        } else {
            $data['type'] = 'message';
            $data['level'] = $level;
            $data['site_id'] = Fun::getSiteId();
            $data['data'] = [
                'messagelog_id' => $id,
                'message_type' => 'sms'
            ];
            /*\Yii::error('messagemanage sendsms function datas:');
            \Yii::error($data);*/
            \Yii::$app->swooleClient->send($data);
        }
    }

}